package com.yuuzy.qqclient.view;

import com.yuuzy.qqclient.service.FileClientService;
import com.yuuzy.qqclient.service.MessageClientService;
import com.yuuzy.qqclient.service.UserClientService;
import com.yuuzy.qqclient.utils.Utility;

import java.io.IOException;

/**
 * 客户端的菜单界面
 */
public class QQView {
	private boolean loop = true;//控制是否显示菜单
	private String key = " ";
	private  UserClientService userClientService = new UserClientService();//对象是用于登录服务/注册用户
	public static void main(String[] args) throws Exception {
		new QQView().mainMenu();
		System.out.println("您已退出系统");
	}

	private void mainMenu() throws Exception {
		while (loop) {
			System.out.println("===========欢迎登录网络通信系统===========");
			System.out.println("\t\t 1 登录系统");
			System.out.println("\t\t 9 退出系统");
			System.out.println("请输入你的选择：");
			key = Utility.readString(1);
			//根据用户的输入来处理不同的逻辑
			switch (key) {
				case "1":
					System.out.print("请输入用户号:");
					String userID = Utility.readString(20);
					System.out.print("请输入密 码:");
					String password = Utility.readString(20);
					//需要到服务器验证用户是否合法
					//编写一个类 UserClientService［用户登录/注册]
					if (userClientService.checkUser(userID,password)) {//进入二级菜单
						System.out.println("===========欢迎(用户" + userID +
								"登陆成功)===========");
						while (loop) {
							System.out.println("===========网络通信系统二级菜单(用户" + userID +
									")===========");
							System.out.println("\t\t 1 显示在线用户列表");
							System.out.println("\t\t 2 群发消息");
							System.out.println("\t\t 3 私聊消息 ");
							System.out.println("\t\t 4 发送文件");
							System.out.println("\t\t 9 退出系统 ");
							System.out.print("请输入你的选择：");
							key = Utility.readString(1);
							switch (key) {
								case "1":
									//写一个方法实现在线用户列表
									userClientService.onlineFriendList();
									break;
								case "2":
									System.out.println("请输入想对大家说的话：");
									String content1 = Utility.readString(100);
									MessageClientService.sendMessageToAll(content1,userID);
									break;
								case "3":
									System.out.print("请输入想聊天的用户号：");
									String getterID = Utility.readString(20);
									System.out.println("请输入想说的话");
									String content2 = Utility.readString(100);
									//写一个方法，将消息发送给服务器
									MessageClientService.sendMessageToOne(content2,userID,getterID);
									break;
								case "4":
									System.out.print("请输入你想把文件发送给的用户：");
									getterID = Utility.readString(20);
									System.out.print("请输入发送文件的路径:");
									String src = Utility.readString(100);
									System.out.print("请输入把文件发送到对应的路径:");
									String dest = Utility.readString(100);
									FileClientService.sendFileToOne(src,dest,getterID,userID);
									break;
								case "9":
									//调用方法，给服务器发送一个退出系统的message
									userClientService.logout();
									loop = false;
									break;
								default:
									System.out.println("输入错误，请重新输入");
									break;
							}
						}
					} else {//登录失败
						System.out.println("===========登录失败===========");
					}
					break;
				case "9":
					loop = false;
					break;
				default:
					System.out.println("输入错误，请重新输入");
					break;
			}
		}
	}
}
